num_enum
Procedural macros to make inter-operation between primitives and enums easier. This crate is no_std compatible.
Turning an enum into a primitive
use IntoPrimitive;
num_enum
's IntoPrimitive
is more type-safe than using as
, because as
will silently truncate - num_enum
only derives From
for exactly the discriminant type of the enum.
Attempting to turn a primitive into an enum with try_from
use TryFromPrimitive;
use TryFrom;
Variant alternatives
Sometimes a single enum variant might be representable by multiple numeric values.
The #[num_enum(alternatives = [..])]
attribute allows you to define additional value alternatives for individual variants.
(The behavior of IntoPrimitive
is unaffected by this attribute, it will always return the canonical value.)
use TryFromPrimitive;
use TryFrom;
Range expressions are also supported for alternatives, but this requires enabling the complex-expressions
feature:
use TryFromPrimitive;
use TryFrom;
Default variant
Sometimes it is desirable to have an Other
variant in an enum that acts as a kind of a wildcard matching all the value not yet covered by other variants.
The #[num_enum(default)]
attribute allows you to mark variant as the default.
(The behavior of IntoPrimitive
is unaffected by this attribute, it will always return the canonical value.)
use TryFromPrimitive;
use TryFrom;
Safely turning a primitive into an exhaustive enum with from_primitive
If your enum has all possible primitive values covered, you can derive FromPrimitive
for it (which auto-implement stdlib's From
):
You can cover all possible values by:
- Having variants for every possible value
- Having a variant marked
#[num_enum(default)]
- Having a variant marked
#[num_enum(catch_all)]
- Having
#[num_enum(alternatives = [...])
s covering values not covered by a variant.
use FromPrimitive;
Catch-all variant
Sometimes it is desirable to have an Other
variant which holds the otherwise un-matched value as a field.
The #[num_enum(catch_all)]
attribute allows you to mark at most one variant for this purpose. The variant it's applied to must be a tuple variant with exactly one field matching the repr
type.
use FromPrimitive;
use TryFrom;
As this is naturally exhaustive, this is only supported for FromPrimitive
, not also TryFromPrimitive
.
Unsafely turning a primitive into an enum with from_unchecked
If you're really certain a conversion will succeed (and have not made use of #[num_enum(default)]
or #[num_enum(alternatives = [..])]
for any of its variants), and want to avoid a small amount of overhead, you can use unsafe code to do this conversion.
Unless you have data showing that the match statement generated in the try_from
above is a bottleneck for you,
you should avoid doing this, as the unsafe code has potential to cause serious memory issues in your program.
use UnsafeFromPrimitive;
unsafe
Optional features
Some enum values may be composed of complex expressions, for example:
To cut down on compile time, these are not supported by default, but if you enable the complex-expressions
feature of your dependency on num_enum
, these should start working.
License
num_enum may be used under your choice of the BSD 3-clause, Apache 2, or MIT license.